# Generated manually for database performance optimization

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('assets', '0020_consumable_model'),
    ]

    operations = [
        # Asset模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_status') = 0,
                'CREATE INDEX idx_asset_status ON assets_asset(status)',
                'SELECT "Index idx_asset_status already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_status ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_category_id') = 0,
                'CREATE INDEX idx_asset_category_id ON assets_asset(category_id)',
                'SELECT "Index idx_asset_category_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_category_id ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_department_id') = 0,
                'CREATE INDEX idx_asset_department_id ON assets_asset(department_id)',
                'SELECT "Index idx_asset_department_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_department_id ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_purchase_date') = 0,
                'CREATE INDEX idx_asset_purchase_date ON assets_asset(purchase_date)',
                'SELECT "Index idx_asset_purchase_date already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_purchase_date ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_created_at') = 0,
                'CREATE INDEX idx_asset_created_at ON assets_asset(created_at)',
                'SELECT "Index idx_asset_created_at already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_created_at ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_number') = 0,
                'CREATE INDEX idx_asset_number ON assets_asset(asset_number)',
                'SELECT "Index idx_asset_number already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_number ON assets_asset;"
        ),
        
        # AssetLog模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetlog' AND index_name = 'idx_assetlog_asset_id') = 0,
                'CREATE INDEX idx_assetlog_asset_id ON assets_assetlog(asset_id)',
                'SELECT "Index idx_assetlog_asset_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetlog_asset_id ON assets_assetlog;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetlog' AND index_name = 'idx_assetlog_created_at') = 0,
                'CREATE INDEX idx_assetlog_created_at ON assets_assetlog(created_at)',
                'SELECT "Index idx_assetlog_created_at already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetlog_created_at ON assets_assetlog;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetlog' AND index_name = 'idx_assetlog_action') = 0,
                'CREATE INDEX idx_assetlog_action ON assets_assetlog(action)',
                'SELECT "Index idx_assetlog_action already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetlog_action ON assets_assetlog;"
        ),
        
        # AssetBorrow模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetborrow' AND index_name = 'idx_assetborrow_status') = 0,
                'CREATE INDEX idx_assetborrow_status ON assets_assetborrow(status)',
                'SELECT "Index idx_assetborrow_status already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetborrow_status ON assets_assetborrow;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetborrow' AND index_name = 'idx_assetborrow_borrower_id') = 0,
                'CREATE INDEX idx_assetborrow_borrower_id ON assets_assetborrow(borrower_id)',
                'SELECT "Index idx_assetborrow_borrower_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetborrow_borrower_id ON assets_assetborrow;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetborrow' AND index_name = 'idx_assetborrow_expected_return') = 0,
                'CREATE INDEX idx_assetborrow_expected_return ON assets_assetborrow(expected_return_date)',
                'SELECT "Index idx_assetborrow_expected_return already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetborrow_expected_return ON assets_assetborrow;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetborrow' AND index_name = 'idx_assetborrow_created_at') = 0,
                'CREATE INDEX idx_assetborrow_created_at ON assets_assetborrow(created_at)',
                'SELECT "Index idx_assetborrow_created_at already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetborrow_created_at ON assets_assetborrow;"
        ),
        
        # MaintenanceRecord模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_maintenancerecord' AND index_name = 'idx_maintenance_asset_id') = 0,
                'CREATE INDEX idx_maintenance_asset_id ON assets_maintenancerecord(asset_id)',
                'SELECT "Index idx_maintenance_asset_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_maintenance_asset_id ON assets_maintenancerecord;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_maintenancerecord' AND index_name = 'idx_maintenance_status') = 0,
                'CREATE INDEX idx_maintenance_status ON assets_maintenancerecord(status)',
                'SELECT "Index idx_maintenance_status already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_maintenance_status ON assets_maintenancerecord;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_maintenancerecord' AND index_name = 'idx_maintenance_created_at') = 0,
                'CREATE INDEX idx_maintenance_created_at ON assets_maintenancerecord(created_at)',
                'SELECT "Index idx_maintenance_created_at already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_maintenance_created_at ON assets_maintenancerecord;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_maintenancerecord' AND index_name = 'idx_maintenance_priority') = 0,
                'CREATE INDEX idx_maintenance_priority ON assets_maintenancerecord(priority)',
                'SELECT "Index idx_maintenance_priority already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_maintenance_priority ON assets_maintenancerecord;"
        ),
        
        # Consumable模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_consumable' AND index_name = 'idx_consumable_warehouse_id') = 0,
                'CREATE INDEX idx_consumable_warehouse_id ON assets_consumable(warehouse_id)',
                'SELECT "Index idx_consumable_warehouse_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_consumable_warehouse_id ON assets_consumable;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_consumable' AND index_name = 'idx_consumable_category_id') = 0,
                'CREATE INDEX idx_consumable_category_id ON assets_consumable(category_id)',
                'SELECT "Index idx_consumable_category_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_consumable_category_id ON assets_consumable;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_consumable' AND index_name = 'idx_consumable_code') = 0,
                'CREATE INDEX idx_consumable_code ON assets_consumable(code)',
                'SELECT "Index idx_consumable_code already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_consumable_code ON assets_consumable;"
        ),
        
        # InventoryLog模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_inventorylog' AND index_name = 'idx_inventorylog_consumable_id') = 0,
                'CREATE INDEX idx_inventorylog_consumable_id ON assets_inventorylog(consumable_id)',
                'SELECT "Index idx_inventorylog_consumable_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_inventorylog_consumable_id ON assets_inventorylog;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_inventorylog' AND index_name = 'idx_inventorylog_created_at') = 0,
                'CREATE INDEX idx_inventorylog_created_at ON assets_inventorylog(created_at)',
                'SELECT "Index idx_inventorylog_created_at already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_inventorylog_created_at ON assets_inventorylog;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_inventorylog' AND index_name = 'idx_inventorylog_transaction_type') = 0,
                'CREATE INDEX idx_inventorylog_transaction_type ON assets_inventorylog(transaction_type)',
                'SELECT "Index idx_inventorylog_transaction_type already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_inventorylog_transaction_type ON assets_inventorylog;"
        ),
        
        # Contract模型索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_contract' AND index_name = 'idx_contract_supplier_id') = 0,
                'CREATE INDEX idx_contract_supplier_id ON assets_contract(supplier_id)',
                'SELECT "Index idx_contract_supplier_id already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_contract_supplier_id ON assets_contract;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_contract' AND index_name = 'idx_contract_status') = 0,
                'CREATE INDEX idx_contract_status ON assets_contract(status)',
                'SELECT "Index idx_contract_status already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_contract_status ON assets_contract;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_contract' AND index_name = 'idx_contract_start_date') = 0,
                'CREATE INDEX idx_contract_start_date ON assets_contract(start_date)',
                'SELECT "Index idx_contract_start_date already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_contract_start_date ON assets_contract;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_contract' AND index_name = 'idx_contract_end_date') = 0,
                'CREATE INDEX idx_contract_end_date ON assets_contract(end_date)',
                'SELECT "Index idx_contract_end_date already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_contract_end_date ON assets_contract;"
        ),
        
        # WorkflowInstance模型索引优化 (如果表存在)
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_workflowinstance') > 0,
                'CREATE INDEX idx_workflow_instance_status ON assets_workflowinstance(status)',
                'SELECT "Table assets_workflowinstance does not exist"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_workflow_instance_status ON assets_workflowinstance;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_workflowinstance') > 0,
                'CREATE INDEX idx_workflow_instance_template_id ON assets_workflowinstance(template_id)',
                'SELECT "Table assets_workflowinstance does not exist"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_workflow_instance_template_id ON assets_workflowinstance;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_workflowinstance') > 0,
                'CREATE INDEX idx_workflow_instance_created_at ON assets_workflowinstance(created_at)',
                'SELECT "Table assets_workflowinstance does not exist"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_workflow_instance_created_at ON assets_workflowinstance;"
        ),
        
        # WorkflowStepTask模型索引优化 (如果表存在)
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_workflowsteptask') > 0,
                'CREATE INDEX idx_workflow_task_assignee_id ON assets_workflowsteptask(assignee_id)',
                'SELECT "Table assets_workflowsteptask does not exist"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_workflow_task_assignee_id ON assets_workflowsteptask;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_workflowsteptask') > 0,
                'CREATE INDEX idx_workflow_task_status ON assets_workflowsteptask(status)',
                'SELECT "Table assets_workflowsteptask does not exist"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_workflow_task_status ON assets_workflowsteptask;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_workflowsteptask') > 0,
                'CREATE INDEX idx_workflow_task_due_date ON assets_workflowsteptask(due_date)',
                'SELECT "Table assets_workflowsteptask does not exist"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_workflow_task_due_date ON assets_workflowsteptask;"
        ),
        
        # 复合索引优化
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_status_category') = 0,
                'CREATE INDEX idx_asset_status_category ON assets_asset(status, category_id)',
                'SELECT "Index idx_asset_status_category already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_status_category ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_asset' AND index_name = 'idx_asset_department_status') = 0,
                'CREATE INDEX idx_asset_department_status ON assets_asset(department_id, status)',
                'SELECT "Index idx_asset_department_status already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_asset_department_status ON assets_asset;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_assetborrow' AND index_name = 'idx_assetborrow_status_borrower') = 0,
                'CREATE INDEX idx_assetborrow_status_borrower ON assets_assetborrow(status, borrower_id)',
                'SELECT "Index idx_assetborrow_status_borrower already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_assetborrow_status_borrower ON assets_assetborrow;"
        ),
        migrations.RunSQL(
            """
            SET @sql = (SELECT IF(
                (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS 
                 WHERE table_schema = DATABASE() AND table_name = 'assets_maintenancerecord' AND index_name = 'idx_maintenance_asset_status') = 0,
                'CREATE INDEX idx_maintenance_asset_status ON assets_maintenancerecord(asset_id, status)',
                'SELECT "Index idx_maintenance_asset_status already exists"'
            ));
            PREPARE stmt FROM @sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="DROP INDEX IF EXISTS idx_maintenance_asset_status ON assets_maintenancerecord;"
        ),
    ]
